<?php
namespace Home\Controller\Mobile;
use Home\Controller\Mobile\BasicController;

class DetailController extends BasicController {
	
	public function detail(){
		$userdata = session('user');
		$uid = $userdata['uid'];//用户id
		$id = I('request.id');//项目id
		
		$par['id'] = $id;
		$list = M('sxkj_offline_project')->where($par)->find();
		
		$par = array();
		$par['flag'] = 'system';
		$system = M('sxkj_offline_data')->where($par)->find();
		$system = json_decode($system['data'],true);
		
		$this->assign('system',$system);
		$this->assign('id',$id);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function do_detail(){
		$userdata = session('user');
		$uid = $userdata['uid'];

		$phone = I('post.phone');
		if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
	        echo '请输入正确的号码';exit;
	    }

		$order_num = date("Ymd").$uid.substr(uniqid(), 7, 13);
		$name = I('post.name');
		$phone = I('post.phone');
		
		//项目id
		$pid = I('post.pid');
		$project = M('sxkj_offline_project')->where('id=%d',$pid)->field('taskid,reprice,commission')->find();
		
		$par['flag'] = 'bill';
        $bill = M('sxkj_offline_data')->where($par)->find();
        $bill = json_decode($bill['data'],true);
		
		$api_key = $bill['apikey'];
		
		$api_url = $bill['url'];
		
		if(empty($api_key)){
			echo '提单系统key不能为空';exit;
		}
		
		//请求地址
		$url = $api_url.'api/tackorder';
		
		//回调地址
		$notify_url = $this->get_http().'://'.$_SERVER['HTTP_HOST'].U('Home/Mobile/Notify/notify_url');	
		
		$data = array();
		
		$data['key'] = $api_key;
		
		$data['taskid'] = $project['taskid'];
		
		$data['order_num'] = $order_num;
		
		$data['name'] = $name;
		
		$data['phone'] = $phone;
		
		$data['notify_url']	= $notify_url;	
		
		$data['commission'] = $project['commission'];

		$res = $this->dopost($url,$data);
		
		$res = json_decode($res,true);
		
		if($res['code']==200){
			//提单系统订单号
			$out_trade_no = $res['data']['out_trade_no'];
			
			$data = array();
			$data['pid'] = $pid;
			$data['uid'] = $uid;
			$data['name'] = $name;
			$data['phone'] = $phone;
			$data['order_num'] = $order_num;
			$data['out_trade_no'] = $out_trade_no;
			$data['status'] = 0;
			$data['reprice'] = $project['reprice'];
			$data['commission'] = $project['commission'];
			$data['add_time'] = time();
			$data['update_time'] = time();
			$res = M('sxkj_offline_order')->add($data);
			if($res!==false){
				echo '1';exit;
			}else{
				echo '2';exit;
			}
		}else{
			echo $res['msg'];	
		}
	}
		
 	public function do_other_detail(){
		$userdata = session('user');
		$uid = $userdata['uid'];

		$order_num = date("Ymd").$uid.substr(uniqid(), 7, 13);
		
		//项目id
		$pid = I('post.pid');
		$project = M('sxkj_offline_project')->where('id=%d',$pid)->field('reprice')->find();
		
		if($project['flag']==1){
			$par_order['pid'] = $pid;
			$par_order['uid'] = $uid;
			$order_count = M('sx_offline_order')->where($par_order)->count();
			if($order_count>0){
				echo '该项目你已经参加过了!';exit;
			}
		}
		
		$img_len = I('post.img_length');
		$img_arr = [];//图片数组
		for($i=0;$i<$img_len;$i++){
			$img_arr[] = $this->do_upload($_FILES['img'.$i]);
		}
		$data['img'] = json_encode($img_arr);
		
		$data['pid'] = $pid;
		$data['uid'] = $uid;
		$data['text'] = I('post.textval');
		$data['order_num'] = $order_num;
		$data['out_trade_no'] = $out_trade_no;
		$data['status'] = 0;
		$data['reprice'] = $project['reprice'];
		$data['add_time'] = time();
		$data['update_time'] = time();
		$res = M('sxkj_offline_order')->add($data);
		if($res!==false){
			echo '1';exit;
		}else{
			echo '2';exit;
		}
	}
	
	//上传图片
 	public function do_upload($files){
 		if(!empty($files['name'])){
 			//路径
 			$path = $this->get_http().'://'.$_SERVER['HTTP_HOST'].__ROOT__.'/Uploads/';
 			//上传本地服务器
 			if($files['name']=='blob'){
 				$this->upload_name = 'dome.jpg';
 			}else{
 				$this->upload_name = $files['name']; 
 			}	
 			$this->upload_filetype = $files["type"];
 			$this->upload_server_name = uniqid().$this->getRandom(4).strrchr($this->upload_name, '.');
 			$this->upload_tmp_address = $files["tmp_name"]; //取得临时地址				
 			$this->upload_file_size = $files["size"]; //上传文件的大小
 			
 			$d_date = date('Y-m-d');
 			
 			$dir = './Uploads/image/'.$d_date.'/';	
 	    	// 判断目录是否存在
 	    	if (!file_exists($dir)){
 	    		// 创建目录
 	     	   	mkdir($dir,0777,true);
 	     	}
 	
 			$this->file_server_address = $dir.$this->upload_server_name;
 			$res = move_uploaded_file($this->upload_tmp_address,$this->file_server_address);//从temp目录移出
 			if($res!==false){
 				$path = $path.'image/'.$d_date.'/'.$this->upload_server_name;
 				return $path;
 			}
 		}
 	}
}